package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.Classes;
import com.example.result.Result;
import com.example.service.ClassService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 班级控制层
 * @date 2019-12-31
 */
@RestController
@RequestMapping("/api")
public class ClassController {

    @Autowired
    ClassService classService;

    //===============================班级管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加班级信息
     */
    @RequestMapping(value = "/classes", method = RequestMethod.POST)
    public Result addClass(@RequestBody String json) {
        //将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Classes classes = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Classes.class);

        return classService.addClass(classes);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新班级信息
     */
    @RequestMapping(value = "/classes", method = RequestMethod.PUT)
    public Result updateClass(@RequestBody String json) {
        //将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Classes classes = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Classes.class);
        return classService.updateClass(classes);

    }

    /**
     * @param classId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据班级编号删除班级信息
     */
    @RequestMapping(value = "/classes/{classId}", method = RequestMethod.DELETE)
    public Result deleteClassById(@PathVariable("classId") String classId) {
        return classService.deleteClassById(classId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有班级信息
     */
    @RequestMapping(value = "/classes", method = RequestMethod.GET)
    public Result queryAllClass() {
        return classService.queryAllClass();

    }

    /**
     * @return 以JSON格式返回所有班级信息和学期信息
     * @describe 查询所有班级信息和学期信息
     */
    @RequestMapping(value = "/classes/all", method = RequestMethod.GET)
    public Result queryAllClassAndTerm() {

        return classService.queryAllClassAndTerm();
    }

    /**
     * @param classId
     * @return 以JSON格式返回该班级信息
     * @describe 根据编号查询班级信息
     */
    @RequestMapping(value = "/classes/{classId}", method = RequestMethod.GET)
    public Result queryClassById(@PathVariable("classId") String classId) {
        return classService.queryClassById(classId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加班级信息
     */
    @RequestMapping(value = "/classes/excels", method = RequestMethod.POST)
    public Result addClassByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<Classes> list = JSON.parseArray(parseObject.getString("json"), Classes.class);

        System.out.println(list);

        return classService.addClassByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据班级编号批量删除班级信息
     */
    @RequestMapping(value = "/classes/dels", method = RequestMethod.POST)
    public Result deleteClassByIds(@RequestBody Map<String, String> Ids) {

        return classService.deleteClassByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 班级信息模糊查询
     */
    @RequestMapping(value = "/classes/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return classService.serachByInfo(info);

    }
}
